package im.yisme.controler;

import java.io.IOException;

import org.json.JSONException;

import im.yisme.model.service.ModelServiceProvider;
import im.yisme.model.service.PlatformService;
import im.yisme.vo.LyricsVO;
import im.yisme.controler.service.PlatformCtrlService;

public class PlatformCtrl implements PlatformCtrlService{

	private static PlatformCtrl ctrl;
	private PlatformService service;
	
	private PlatformCtrl(){
		service = ModelServiceProvider.getPlatformService();
	}
	
	public static PlatformCtrl getInstance(){
		return ctrl == null ? ctrl = new PlatformCtrl() : ctrl;
	}
	
	public void search(String keyword) {
		// TODO Auto-generated method stub
		try {
			service.search(keyword);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean evaluate(long id, int score) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			flag = service.evaluate(id, score);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
	
	public boolean upload(long id) {
		// TODO Auto-generated method stub
		boolean flag=false;
		
		try {
			long remoteId = service.uploadInfo(id);
			flag = service.uploadFile(id, remoteId);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

	public Long download(long remoteId) {
		// TODO Auto-generated method stub
		long id = 0;
		try {
			LyricsCtrl lyricsCtrl = LyricsCtrl.getInstance();
			LyricsVO toAdd = service.downloadInfo(remoteId);
			
			id = lyricsCtrl.addLyricsToRepository(toAdd);
			service.downloadFile(id, toAdd.getFilePath());
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return id;
	}
}
